package com.gsit.misn.bilders;

import static com.gsit.misn.utils.ScreenShotUtil.captureScreenShot;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gsit.misn.domain.Misn;
import com.gsit.misn.domain.Patient;
import com.gsit.misn.domain.User;
import com.gsit.misn.utils.ExcelWriter;
import com.gsit.misn.utils.Writer;
import com.thoughtworks.selenium.DefaultSelenium;

public class LoadPatientBilder implements PatientBilder<Misn> {

	private static Logger logger = LoggerFactory
			.getLogger(LoadPatientBilder.class);

	private List<Patient> patients ;
	private final Integer noOfRecordPerPage = 10;
	private Integer currNoOfRecords = 0;
	private String searchURL;
	private Integer rowNo;
	private Writer<User, List<Patient>> writer;
	private Misn misn;
	
	public LoadPatientBilder(){
		patients = new ArrayList<Patient>();
		writer = new ExcelWriter();
	}

	@Override
	public void excute(DefaultSelenium selenium, final Misn misn) {

		this.misn = misn;
		Integer totalLoops = misn.getUser().getTotalNoOfPages()/noOfRecordPerPage;
		//Integer totalLoops = 1;
		Integer startPage =0;
		currNoOfRecords= startPage*10;
		
		for (int page = startPage; page < totalLoops+1; page++) {
			this.pageNavigation(selenium,page);
		}
		
		this.creatExcelForPatient();
	}

	private void pageNavigation(DefaultSelenium selenium, int pageNo) {

		try {
			for (int row = 2; row < noOfRecordPerPage + 2; row++) {
				
				currNoOfRecords++;
				System.out.println("The curr :: "+currNoOfRecords + "  :: Total no of pages :: "+misn.getUser().getTotalNoOfPages());
				if (currNoOfRecords.equals(misn.getUser().getTotalNoOfPages()+1)) {

					System.out.println("The record is finished ...");
					break;
				} else {
					System.out.println("Record no [ " + currNoOfRecords
							+ " ]  is processing ........");
				}
				searchURL = misn.getMisnLinks().getSearchUrl() + "?pageNum_r="+ pageNo + "&totalRows_r="+ misn.getUser().getTotalNoOfPages();
				selenium.open(searchURL);
				selenium.waitForPageToLoad("10000");
				rowNo = (pageNo * noOfRecordPerPage) + row - 2;
				String clickLink = "//table//tr[" + row + "]/td[3]/a";
				if(selenium.isElementPresent(clickLink)){
					selenium.click(clickLink);
					selenium.waitForPageToLoad("20000");
					createPatient(selenium);
				}else{
					System.out.println("No load link found.");
				}
			}
		} catch (Exception e) {
			logger.error("Error is in selenium object retriving...");
			e.printStackTrace();
		}

	}

	private void createPatient(DefaultSelenium selenium) {
		try {
			Patient patient= new Patient(selenium, searchURL, rowNo);
			patients.add(patient);
			if(misn.getScreenShotCapture()){
				captureScreenShot(patient.getPatientID());
			}
		} catch (Exception e) {
			logger.error("Error is in selenium object processing");
			e.printStackTrace();
		}

	}

	private void creatExcelForPatient() {
		try {
			writer.write(misn.getUser(), patients);

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}
